import { EventEmitter, Injectable, Output } from '@angular/core'

@Injectable()
export class TabService {
  tabs = [
    {
      path: '/dashboard',
      data: {
        title: '公告板'
      }
    }
  ]
  @Output() addChange: EventEmitter<any> = new EventEmitter()

  constructor() { }

  setTabs(tabs): void {
    this.tabs = tabs
  }

  getTabs(): Array<object> {
    return this.tabs
  }

  addTab(item): any {
    const hasTab = this.tabs.some(tab => tab.path === item.path)

    if (!hasTab) {
      this.tabs.push({
        path: item.path,
        data: item.data
      })
      this.tabs = Array.from(new Set(this.tabs))
    }
    this.addChange.emit(this.tabs)
  }
}
